package net.eascn.babyClone.utility
{
	import flash.utils.getDefinitionByName;
	
	import net.eascn.babyClone.model.ModelLocator;
	import net.eascn.babyClone.vo.AlbumVO;
	import net.eascn.babyClone.vo.BackgroundVO;
	import net.eascn.babyClone.vo.LayoutVO;
	import net.eascn.babyClone.vo.PageVO;
	import net.eascn.babyClone.vo.PhotoBorderVO;
	import net.eascn.babyClone.vo.PhotoVO;
	
	public class VoXMLUtil
	{
		public function VoXMLUtil()
		{
		}
		
		
		public static function parseBorderVO(itemXML:XML):PhotoBorderVO
		{
			var borderVO:PhotoBorderVO = new PhotoBorderVO();
				if(!itemXML)
					return borderVO;	
				borderVO.id = itemXML.@id;
				borderVO.title = itemXML.@title;
				var filtersList:XMLList = itemXML.filters.children();
				
				for each(var filterXML:XML  in filtersList)
				{
					var filterClassName:String = filterXML.@className;
					var filterClass:Class  = getDefinitionByName(filterClassName) as Class;
					var filterInstance:* = new filterClass();
					var attrisList:XMLList = filterXML.attributes();
					for each(var attrisXML:XML in attrisList)
					{
						var attName:String = attrisXML.name();
						if(attName in filterInstance)
						{
							filterInstance[attName] = attrisXML.toString();
						}
					}
					borderVO.filters.push(filterInstance);
				}
				borderVO.data = itemXML;
				return borderVO;
		}
		public static function parseFrameDescXML(xml:XML):Array
		{
			var result:Array = [];
			var itemList:XMLList = xml.children();
			for each(var itemXML:XML  in itemList)
			{
				
				result.push(parseBorderVO(itemXML));
			}
			
			return result;	
		}
		
		public static function parsePhotoDescXML(xml:XML):Array
		{
			var result:Array = [];
			var itemList:XMLList = xml.photo;
			for each(var itemXML:XML  in itemList)
			{
				var itemPhotoVO:PhotoVO = new PhotoVO();
				itemPhotoVO.id = itemXML.photoid.toString();
				itemPhotoVO.original = itemXML.photob.toString();
				itemPhotoVO.thumbnails  = itemXML.photos.toString();
				itemPhotoVO.title = itemXML.photoid.toString();
				itemPhotoVO.data = itemXML;
				result.push(itemPhotoVO);
			}
			
			return result;
		}
		
		public static function parseBackGroundDescXML(xml:XML):Array
		{
			var result:Array = [];
			var itemList:XMLList  = xml.children();
			for each(var itemXML:XML in itemList)
			{
				var bvo:BackgroundVO = new BackgroundVO();
				bvo.id = itemXML.@id;
				bvo.original  = itemXML.original.toString();
				bvo.thumbnails = itemXML.thumbnails.toString();
				bvo.title = itemXML.@title;
				result.push(bvo);
			}
			return result;
		}
		
		public static function parseLayoutDescXML(xml:XML):Array
		{
			var result:Array  = [];
			var itemList:XMLList = xml.children()
			for each(var itemXML:XML  in itemList)
			{
				var layoutVO:LayoutVO = new LayoutVO;
				layoutVO.id  = itemXML.@id;
				layoutVO.thumbnails = itemXML.@thumbnails;
				layoutVO.title = itemXML.@title;
				layoutVO.data = itemXML;
				var compList:XMLList = itemXML.children();
				for each(var compXML:XML  in compList)
				{
					var obj:Object = {};
					var	compAttr:XMLList = compXML.attributes();
					for each(var attr:XML  in compAttr)
					{
						var attName:String = attr.localName();
						obj[attName] = attr.toString();
					}
					layoutVO.comps.push(obj);
				}
				result.push(layoutVO);
			}
			
			return result;
		}
		
		public static function parseAlbumDefinedDescXML(xml:XML):AlbumVO
		{
			var result:AlbumVO= new AlbumVO();
			result.id = xml.@id;
			result.title = xml.@title;
			var pageList:XMLList = xml.children();
			for each(var pageXml:XML  in pageList)
			{
				var pageVO:PageVO = new PageVO();
				pageVO.id = pageXml.@id;
				pageVO.title = pageXml.@title;
				result.pages.push(pageVO);
			}
			
			return result;
		}
		
		public static function getLayoutVOById(id:String):LayoutVO
		{
			var model:ModelLocator = ModelLocator.getInstance();
			for each(var layout:LayoutVO  in model.layoutList)
			{
				if(layout.id ==id)
					return layout;
			}
			return null;
		}
		
		public static function getBackgroundVOById(id:String):BackgroundVO
		{
			var model:ModelLocator = ModelLocator.getInstance();
			for each(var bg:BackgroundVO in model.backgroundList)
			{
				if(bg.id == id)
					return bg;
			}
			return null;
		}
		
		
		public static function xml2vo(xml:XML,voClass:Class):*
		{
			//for each attri to vo's prop
			
		}
		
		public static function vo2xml():void
		{
			//for each vo's prop to xml's attri/ prop
		}
	}
}